//
//  RecStoryDetailsViewController.m
//  TravelEveryDay
//
//  Created by dlios on 15-10-19.
//  Copyright (c) 2015年 com.lanou3g. All rights reserved.
//

#import "RecStoryDetailsViewController.h"
#import "RecStoryDetailsTableViewCell.h"
#import "RecStoryDetailsModel.h"
#import "RecUserViewController.h"
@interface RecStoryDetailsViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation RecStoryDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    _dataArr = [[NSMutableArray alloc] init];
    [self getData];
}
#pragma mark - 页面创建
-(void)setUpPage
{
    _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 130)];
    _table.delegate = self;
    _table.dataSource = self;
    [self.view addSubview:_table];
    
    //最上面描述label
    UILabel *userDescLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    userDescLabel.text = self.name;
    [userDescLabel setNumberOfLines:0];
    userDescLabel.font = [UIFont boldSystemFontOfSize:18.];
    _userDescHeight = [StringWithHeight heightWithText:userDescLabel.text width:SCREEN_WIDTH - 20 font:[UIFont boldSystemFontOfSize:18.]];
    userDescLabel.frame = CGRectMake(10, 160, SCREEN_WIDTH - 20, _userDescHeight);

    //头部view
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 170 + _userDescHeight)];
    _table.tableHeaderView = headerView;
    
    
    //头像image
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 50, 50)];
    image.layer.cornerRadius = 25;
    image.layer.masksToBounds = YES;
    [image sd_setImageWithURL:[NSURL URLWithString:self.userModel.avatar_l] placeholderImage:PLACEHOLDERIMAGE];
    image.backgroundColor = [UIColor blueColor];
    [image setUserInteractionEnabled:YES];
    [headerView addSubview:image];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userDetails)];
    [image addGestureRecognizer:tap];
    
    //用户名字
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(90, 20, 200, 50)];
    nameLabel.text = self.userModel.name;
    [headerView addSubview:nameLabel];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(10, 100, SCREEN_WIDTH - 20, 1)];
    lineView.backgroundColor = [UIColor grayColor];
    lineView.alpha = 0.6;
    [headerView addSubview:lineView];
    [headerView addSubview:userDescLabel];
    
    //底部view
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
    _table.tableFooterView = footerView;
    
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 20, SCREEN_WIDTH - 100, 40)];
    label.text = [NSString stringWithFormat:@"故事收录于 %@",self.text];
    [footerView addSubview:label];
    
    UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 60, SCREEN_WIDTH - 100, 40)];
    timeLabel.text = self.time;
    timeLabel.alpha = 0.5;
    [footerView addSubview:timeLabel];
}
#pragma mark - 手势点击方法
-(void)userDetails
{
    RecUserViewController *user = [[RecUserViewController alloc] init];
    [self.navigationController pushViewController:user animated:YES];
    user.userId = self.userId;
}
#pragma mark - tableview Delegate & Datasource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RecStoryDetailsModel *model = [_dataArr objectAtIndex:indexPath.row];
    static NSString *cellID = @"cell";
    RecStoryDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[RecStoryDetailsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    [cell getStoryData:model];
    return cell;
   
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RecStoryDetailsModel *model = [_dataArr objectAtIndex:indexPath.row];
    if ([model.photo_width.stringValue isEqualToString:@""]|| [model.photo_height.stringValue isEqualToString:@""]) {
        _imageHeight = 0;
    }else{
        _imageHeight = 250 * model.photo_height.integerValue / model.photo_width.integerValue;
    }
  _textHeight = [StringWithHeight heightWithText:model.text width:SCREEN_WIDTH - 20 font:[UIFont systemFontOfSize:16.]];
    return _imageHeight + _textHeight + 50;
}
#pragma mark - 数据获取
-(void)getData
{
    [Refresh getdataWithString:[NSString stringWithFormat:@"http://api.breadtrip.com/v2/new_trip/spot/?spot_id=%@",self.myId] block:^(id block) {
        
        //描述
        self.name = [[[block objectForKey:@"data"]objectForKey:@"spot"]objectForKey:@"text"];
        
        //收录于
        self.text = [[[block objectForKey:@"data"]objectForKey:@"trip"]objectForKey:@"name"];
        //时间
        self.time = [[[[block objectForKey:@"data"]objectForKey:@"trip"]objectForKey:@"date_added"] substringWithRange:NSMakeRange(0, 10)];
        self.userId = [[[[block objectForKey:@"data"]objectForKey:@"trip"]objectForKey:@"user"]objectForKey:@"id"];
       
        for (NSDictionary *dic in [[[block objectForKey:@"data"]objectForKey:@"spot"]objectForKey:@"detail_list"]) {
            RecStoryDetailsModel *model = [RecStoryDetailsModel modelWithDictionary:dic];
            [_dataArr addObject:model];
            
            _imageHeight = 250 * (model.photo_height.integerValue / model.photo_width.integerValue);
        }
        [self setUpPage];
       
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
